﻿--[[
standardize my .ass subtitles, I'd like to use STHeiti for Chinese, Segoe UI for English.
I also need some style for music
--]]

local tr = aegisub.gettext

script_name = tr"standardize"
script_description = tr"standardize my .ass subtitles"
script_author = "Syler Zhou"
script_version = "0.70"
script_modified = "24 March 2016"

alternative_style_name="SecondStyle"
music_style_name="Music"
default_style_name="Default"
top_style_name="Top"
default_font_size=12
second_font_size=15
music_font_size=15

standard_default_style={
			class = "style",
			section = "V4+ Styles",
			name = default_style_name,
			fontname = "STHeiti",
			fontsize = default_font_size,
			color1 = "&H00EFD98D&",
			color2 = "&HF0000000&",
			color3 = "&H000D0500&",
			color4 = "&H00000000&",
			bold = false,
			italic = false,
			underline = false,
			strikeout = false,
			scale_x = 100,
			scale_y = 100,
			spacing = 0,
			angle = 0,
			borderstyle = 1,
			outline = 2,
			shadow = 1,
			align = 2,
			margin_l = 0,
			margin_r = 0,
			margin_t = 4,
			margin_b = 0,
			encoding = 1
		}

standard_top_style={
			class = "style",
			section = "V4+ Styles",
			name = top_style_name,
			fontname = "STHeiti",
			fontsize = default_font_size,
			color1 = "&H00EFD98D&",
			color2 = "&HF0000000&",
			color3 = "&H000D0500&",
			color4 = "&H00000000&",
			bold = false,
			italic = false,
			underline = false,
			strikeout = false,
			scale_x = 100,
			scale_y = 100,
			spacing = 0,
			angle = 0,
			borderstyle = 1,
			outline = 2,
			shadow = 1,
			align = 8,
			margin_l = 0,
			margin_r = 0,
			margin_t = 4,
			margin_b = 0,
			encoding = 1
		}
standard_music_style={
			class = "style",
			section = "V4+ Styles",
			name = music_style_name,
			fontname = "Segoe UI",
			fontsize = second_font_size,
			color1 = "&H006523DE&",
			color2 = "&H001BA818&",
			color3 = "&H003410EA&",
			color4 = "&H00000000&",
			bold = false,
			italic = false,
			underline = false,
			strikeout = false,
			scale_x = 100,
			scale_y = 100,
			spacing = 0,
			angle = 0,
			borderstyle = 1,
			outline = 1,
			shadow = 1,
			align = 2,
			margin_l = 0,
			margin_r = 0,
			margin_t = 4,
			margin_b = 0,
			encoding = 1
		}
standard_alternative_style={
			class = "style",
			section = "V4+ Styles",
			name = alternative_style_name,
			fontname = "Segoe UI",
			fontsize = music_font_size,
			color1 = "&H003CF1F3&",
			color2 = "&H001BA818&",
			color3 = "&H00000000&",
			color4 = "&H00000000&",
			bold = false,
			italic = false,
			underline = false,
			strikeout = false,
			scale_x = 100,
			scale_y = 100,
			spacing = 0,
			angle = 0,
			borderstyle = 1,
			outline = 2,
			shadow = 1,
			align = 2,
			margin_l = 0,
			margin_r = 0,
			margin_t = 8,
			margin_b = 0,
			encoding = 1
		}

function UpdateStyle(origin_style,standard_style)
	local ischanged=false

	if origin_style.name~=standard_style.name then
		origin_style.name=standard_style.name
		ischanged=true
	end

	if origin_style.fontname~=standard_style.fontname then
		origin_style.fontname=standard_style.fontname
		ischanged=true
	end

	if origin_style.fontsize~=standard_style.fontsize then
		origin_style.fontsize=standard_style.fontsize
		ischanged=true
	end

	if origin_style.color1~=standard_style.color1 then
		origin_style.color1=standard_style.color1
		ischanged=true
	end

	if origin_style.color2~=standard_style.color2 then
		origin_style.color2=standard_style.color2
		ischanged=true
	end

	if origin_style.color3~=standard_style.color3 then
		origin_style.color3=standard_style.color3
		ischanged=true
	end

	if origin_style.color4 ~=standard_style.color4 then
		origin_style.color4 =standard_style.color4
		ischanged=true
	end

	if origin_style.bold ~= standard_style.bold then
		origin_style.bold = standard_style.bold
		ischanged=true
	end

	if origin_style.italic ~= standard_style.italic then
		origin_style.italic = standard_style.italic
		ischanged=true
	end

	if origin_style.underline ~= standard_style.underline then
		origin_style.underline = standard_style.underline
		ischanged=true
	end

	if origin_style.strikeout ~= standard_style.strikeout then
		origin_style.strikeout = standard_style.strikeout
		ischanged=true
	end

	if origin_style.scale_x ~= standard_style.scale_x then
		origin_style.scale_x = standard_style.scale_x
		ischanged=true
	end

	if origin_style.scale_y ~= standard_style.scale_y then
		origin_style.scale_y = standard_style.scale_y
		ischanged=true
	end

	if origin_style.spacing ~= standard_style.spacing then
		origin_style.spacing = standard_style.spacing
		ischanged=true
	end

	if origin_style.angle ~= standard_style.angle then
		origin_style.angle = standard_style.angle
		ischanged=true
	end

	if origin_style.borderstyle ~= standard_style.borderstyle then
		origin_style.borderstyle = standard_style.borderstyle
		ischanged=true
	end

	if origin_style.outline ~= standard_style.outline then
		origin_style.outline = standard_style.outline
		ischanged=true
	end

	if origin_style.shadow ~= standard_style.shadow then
		origin_style.shadow = standard_style.shadow
		ischanged=true
	end

	if origin_style.align ~= standard_style.align then
		origin_style.align = standard_style.align
		ischanged=true
	end

	if origin_style.margin_l ~= standard_style.margin_l then
		origin_style.margin_l = standard_style.margin_l
		ischanged=true
	end

	if origin_style.margin_r ~= standard_style.margin_r then
		origin_style.margin_r = standard_style.margin_r
		ischanged=true
	end

	if origin_style.margin_t ~= standard_style.margin_t then
		origin_style.margin_t = standard_style.margin_t
		ischanged=true
	end

	--[[if origin_style.margin_b ~= standard_style.margin_b then
		origin_style.margin_b = standard_style.margin_b
		ischanged=true
	end--]]

	if origin_style.encoding ~= standard_style.encoding then
		origin_style.encoding = standard_style.encoding
		ischanged=true
	end

	return ischanged,origin_style
end

function GetConfiguration()
local button,dialog, result_table
	dialog=
	{
		{
			class="label",name="lbltips", x=1,y=0,width=2,height=2,label="**Note** Please backup your subtitle file to a safe place first."
		},
	   {
			class="label",name="lbldefaultname", x=1,y=2,width=1,height=1,label="default style name"
	   },
	   {
			class="edit",name="txtdefaultname",x=2,y=2,width=1,height=1,text=default_style_name,hint="the default style name, usually it's \"Default\", somebody likes \"*Default\""
	   }
	}

	button, result_table = aegisub.dialog.display(dialog,{"Do it", "Give up"},{ok="Do it",cancel="Give up"})

	default_style_name = result_table["txtdefaultname"]
	standard_default_style.name = default_style_name

	if button==false then
		aegisub.cancel()
	end

end

function standardize(subtitle)
	local nline = #subtitle
	local i=1
	local last_style_index=1
	local default_style_line,default_style_line_index
	local music_style_line,music_style_line_index
	local alternative_style_line,alternative_style_line_index
	local top_style_line_index, top_style_line
	local last_section_position = -1
	local found_Play_Resolution = false
	local found_ScaledBorderAndShadow,scaledBorderAndShadow_line_index = false,0
	local PlayResXPosition,PlayResYPosition=0,0

	GetConfiguration()

	while i<=nline do
		aegisub.progress.set(i * 100 / nline)
		if subtitle[i].class=="info" then
			if subtitle[i].section == "[Script Info]" then
				--[[
				for k,v in pairs(subtitle[i]) do
					aegisub.log(string.format("%s:%s\r\n",tostring(k),tostring(v)))
				end
				aegisub.log("----------\r\n")
				--]]

				last_section_position = i
				if subtitle[i].key == "PlayResX" then
					found_Play_Resolution = true
					PlayResXPosition=i
				end

				if subtitle[i].key == "PlayResY" then
					PlayResYPosition=i
				end

				if subtitle[i].key == "ScaledBorderAndShadow" then
					found_ScaledBorderAndShadow = true
					scaledBorderAndShadow_line_index = i
				end
			end
		end

		if subtitle[i].class=="style" then
			last_style_index=i

			if subtitle[i].name==alternative_style_name then
				alternative_style_line_index=i
				alternative_style_line=subtitle[i]
			end

			if subtitle[i].name==default_style_name then
				default_style_line_index=i
				default_style_name=subtitle[i].name
				default_style_line=subtitle[i]
			end

			if subtitle[i].name==music_style_name then
				music_style_line_index=i
				music_style_line=subtitle[i]
			end

			if subtitle[i].name==top_style_name then
				top_style_line_index=i
				top_style_line=subtitle[i]
			end
		end

		i=i+1
	end

	if default_style_line==nil then
		subtitle.insert(last_style_index+1,standard_default_style)
	else
		local updated,new_default_style = UpdateStyle(default_style_line,standard_default_style)
		if updated==true then
			subtitle[default_style_line_index]=new_default_style
		end
	end

	if music_style_line==nil then
		subtitle.insert(last_style_index+1,standard_music_style)
	else
		local updated,new_music_style = UpdateStyle(music_style_line,standard_music_style)
		if updated==true then
			subtitle[music_style_line_index]=new_music_style
		end
	end

	if alternative_style_line==nil then
		subtitle.insert(last_style_index+1,standard_alternative_style)
	else
		local updated,new_alternative_style = UpdateStyle(alternative_style_line,standard_alternative_style)
		if updated==true then
			subtitle[alternative_style_line_index]=new_alternative_style
		end
	end

	if top_style_line==nil then
		subtitle.insert(last_style_index+1,standard_top_style)
	else
		local updated,new_top_style = UpdateStyle(top_style_line,standard_top_style)
		if updated==true then
			subtitle[top_style_line_index]=new_top_style
		end
	end

	if found_Play_Resolution ==true then
		if subtitle[PlayResXPosition].value=="0" or subtitle[PlayResYPosition].value=="0" then
			found_Play_Resolution=false
			if PlayResXPosition>0 then
				subtitle.delete(PlayResXPosition)
			end

			if PlayResYPosition>0 then
				if PlayResXPosition < PlayResYPosition then
					subtitle.delete(PlayResYPosition-1)
				else
					subtitle.delete(PlayResYPosition)
				end
			end
		end
	end
	if found_Play_Resolution ~=true then
		subtitle.insert(last_section_position + 1, {key="PlayResX",value="384", section="[Script Info]", class="info"})
		subtitle.insert(last_section_position + 2, {key="PlayResY",value="288", section="[Script Info]", class="info"})
		last_section_position = last_section_position + 2
	end

	if found_ScaledBorderAndShadow ~=true then
		subtitle.insert(last_section_position + 1,{key="ScaledBorderAndShadow",value="no", section="[Script Info]", class="info"})
		last_section_position = last_section_position + 1
	else
		if(subtitle[scaledBorderAndShadow_line_index].value ~= "no") then
			scaleline = subtitle[scaledBorderAndShadow_line_index]
			scaleline.value = "no"
			subtitle[scaledBorderAndShadow_line_index]=scaleline
		end
	end

	aegisub.progress.task("Finished!")
	aegisub.progress.set(100)
	--aegisub.log(unicode.to_fold_case("test"))
end

function standardize_subtitle_macro(subtitles, selected_lines, active_line)
	standardize(subtitles)
	aegisub.set_undo_point(script_name)
end

aegisub.register_macro(script_name, script_description, standardize_subtitle_macro)
